﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;

namespace WebApplication1.Model
{
    /// <summary>
    /// 返回消息
    /// </summary>
    public class JsonMessage
    {
        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success { get; set; }
        /// <summary>
        /// 结果编码
        /// 成功：1、警告：0、错误：-1
        /// </summary>
        public string code { get; set; }
        /// <summary>
        /// 结果消息
        /// </summary>
        public string info { get; set; }
        /// <summary>
        /// 传递的数据
        /// </summary>
        public object data { get; set; }

        public override string ToString()
        {
            var iso = new IsoDateTimeConverter();
            iso.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
            return JsonConvert.SerializeObject(this,Formatting.Indented, iso);
            //return JsonConvert.SerializeObject(this);
        }

        ///// <summary>
        ///// 返回HttpResponseMessage对象
        ///// Webapi中调用
        ///// </summary>
        ///// <returns></returns>
        //public HttpResponseMessage ToHttpResponseMessage()
        //{
        //    HttpResponseMessage result = new HttpResponseMessage();
        //    result.Content = new StringContent(this.ToJson(), Encoding.GetEncoding("UTF-8"), "application/json");
        //    return result;
        //}

        /// <summary>
        /// 成功调用
        /// WK
        /// </summary>
        /// <param name="data"></param>
        /// <param name="message"></param>
        public void Successes(object data = null, string message = "")
        {
            this.Success = true;
            this.code = "1";
            this.info = "操作成功！";
            if (data != null)
            {
                this.data = data;
            }
            if (message != "")
            {
                info = message;
            }
        }
        /// <summary>
        /// 失败调用
        /// WK
        /// </summary>
        /// <param name="message"></param>
        public void Faild(string message = "", object data = null)
        {
            this.Success = false;
            this.code = "0";
            this.info = "操作失败！";
            if (message != "")
            {
                if (message.Contains("将截断字符串或二进制数据")) message = "超出字符长度限制";
                info = message;
            }
            if (data != null)
            {
                this.data = data;
            }
        }
        /// <summary>
        /// 失效调用
        /// CB
        /// </summary>
        /// <param name="message"></param>
        public void Expire(string message = "")
        {
            this.Success = false;
            this.code = "401";
            this.info = "操作失败！";
            if (message != "")
            {
                info = message;
            }
        }
        /// <summary>
        /// 失效调用
        /// CB
        /// </summary>
        /// <param name="message"></param>
        public void Skip(string message = "")
        {
            this.Success = false;
            this.code = "2";
            this.info = "";
            if (message != "")
            {
                info = message;
            }
        }
        /// <summary>
        /// 非法请求
        /// </summary>
        public void Illegal(string message = "")
        {
            this.Success = false;
            this.code = "-2";
            this.info = "非法请求！";
            if (message != "")
            {
                info = message;
            }
        }
    }
}
